package trans.utils;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sessionFactory;   
    private static ThreadLocal<Session> session = new ThreadLocal<Session>();   
	
	private HibernateUtil() {   
    }   
 
   static {   
        Configuration cfg = new Configuration();   
        cfg.configure();   
        sessionFactory = cfg.buildSessionFactory();   
    }   
 
   public static Session getThreadLocalSession() {   
        Session s =  session.get();   
       if (s == null) {   
            s = getSession();   
            session.set(s);   
        }   
       return s;   
    }   
 
   public static void closeSession() {   
        Session s =  session.get();   
       if (s != null) {   
            s.close();   
            session.set(null);   
        }   
    }   
 
   public static SessionFactory getSessionFactory() {   
       return sessionFactory;   
    }   
 
   public static Session getSession() {   
       return sessionFactory.openSession();   
    }   
   
   public static void saveOrUpdate(Object entity) {   
       Session s = null;   
       Transaction tx = null;   
      try {   
           s = HibernateUtil.getSession();   
           tx = s.beginTransaction();   
           s.saveOrUpdate(entity);   
           tx.commit();   
       } finally {   
          if (s != null)   
               s.close();   
       }   
   }   
 
   public static void add(Object entity) {   
        Session s = null;   
        Transaction tx = null;   
       try {   
            s = HibernateUtil.getSession();   
            tx = s.beginTransaction();   
            s.save(entity);   
            tx.commit();   
        } finally {   
           if (s != null)   
                s.close();   
        }   
    }   
 
   public static void update(Object entity) {   
        Session s = null;   
        Transaction tx = null;   
       try {   
            s = HibernateUtil.getSession();   
            tx = s.beginTransaction();   
            s.update(entity);   
            tx.commit();   
        } finally {   
           if (s != null)   
                s.close();   
        }   
    }   
 
   public static void delete(Object entity) {   
        Session s = null;   
        Transaction tx = null;   
       try {   
            s = HibernateUtil.getSession();   
            tx = s.beginTransaction();   
            s.delete(entity);   
            tx.commit();   
        } finally {   
           if (s != null)   
                s.close();   
        }   
    }   
 
   public static Object get(@SuppressWarnings("rawtypes") Class clazz, Serializable id) {   
        Session s = null;   
       try {   
            s = HibernateUtil.getSession();   
            Object obj = s.get(clazz, id);   
           return obj;   
        } finally {   
           if (s != null)   
                s.close();   
        }   
    }   
}
